<!-- 来自买家的退款申请 -->
<template>
  <el-card>
    <div class="table">

      <!-- 表格 -->
      <el-table v-loading="loading" :data="returnGoodsRequestList" style="width: 100%">

        <el-table-column v-for="item in launchRequestTableLabel" :key="item.prop" :label="item.label" :prop="item.prop"
          :width="item.width" :sortable="item.sortable" />
        <!-- 申请状态 -->
        <el-table-column :filters="[{ text: '已通过', value: 1 }, { text: '申请中', value: 0 }, { text: '不通过', value: 2 }]"
          prop="status" label="申请状态" :filter-method="filterTag" width="120">
          <template v-slot="scope">
            <el-tag size="small" v-if="scope.row.status === 1" type="success">已通过</el-tag>
            <el-tag size="small" v-else-if="scope.row.status === 0" type="warning">申请中</el-tag>
            <el-tag size="small" v-else type="danger">不通过</el-tag>
          </template>
        </el-table-column>
        <!-- 相关操作 -->
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template v-slot="scope">
            <el-button type="primary" size="small" @click="getReturnGoodsRequestDetail(scope.$index)">详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination background layout="prev, pager, next" :total="config.total" class="pager"
        @current-change="changePage" :page-size="config.size" />
    </div>
  </el-card>

  <!-- 查看退货退款申请详情的模态框 -->
  <el-dialog v-model="dialogVisible" title="查看退货退款申请详情" width="50%">

    <!-- 商品详细信息展示表格 -->
    <el-descriptions :column="1" size="default" border>
      <el-descriptions-item min-width="130px" label="退款申请ID" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.returnGoodsRequestId }}
      </el-descriptions-item>
      <el-descriptions-item label="商店ID" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.storeId }}
      </el-descriptions-item>
      <el-descriptions-item label="订单ID" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.orderId }}
      </el-descriptions-item>
      <el-descriptions-item label="用户ID" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.userId }}
      </el-descriptions-item>
      <el-descriptions-item label="申请发起时间" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.initiateTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请审核时间" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.processTime }}
      </el-descriptions-item>
      <el-descriptions-item label="退款退货理由" label-align="center" align="center">
        {{ returnGoodsRequestDetail.returnGoodsRequest.returnReason }}
      </el-descriptions-item>
      <el-descriptions-item label="申请状态" label-align="center" align="center">
        <el-select v-model="returnGoodsRequestDetail.returnGoodsRequest.status" placeholder="Select">
          <el-option label="申请中" :value="0" :disabled="true" />
          <el-option label="通过" :value="1" />
          <el-option label="不通过" :value="2" />
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="审核意见" label-align="center">
        <el-input v-model="returnGoodsRequestDetail.returnGoodsRequest.info" type="textarea" placeholder="请输入审核意见" />
      </el-descriptions-item>
    </el-descriptions>

    <!-- 底部的按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button :disabled="originStatus !== 0" type="primary" @click="handleProcessRequest()">
          保存审核
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { getCurrentInstance, onMounted, ref, reactive } from 'vue';
import { useStore } from 'vuex';

const store = useStore();
const { proxy } = getCurrentInstance();
// 原本的申请状态
const originStatus = ref(0);
const detailIndex = ref(0);
const manageStoreId = ref("");
const returnGoodsRequestList = ref([]);
// 和当前launchRequestList中的条目相对应
const currentCategoryList = ref([]);
// 控制表格加载遮罩窗是否出现
const loading = ref(false);
// 控制模态框的显示和隐藏
const dialogVisible = ref(false)
// 控制分页
const config = reactive({
  total: 0,  // 总的条目数
  current: 1,  // 当前页
  size: 15  // 一页的条目数
})

const launchRequestTableLabel = reactive([
  {
    prop: "returnGoodsRequestId",
    label: "退款退货申请ID",
    // width: 200,
    sortable: true,
  },
  {
    prop: "storeId",
    label: "商店ID",
    // width: 200,
  },
  {
    prop: "userId",
    label: "用户ID",
    // width: 200,
  },
  {
    prop: "orderId",
    label: "订单ID",
    // width: 200,
  },
  {
    prop: "initiateTime",
    label: "发起时间",
    sortable: true,
    // width: 200,
  },
  {
    prop: "processTime",
    label: "处理时间",
    sortable: true,
    // width: 200,
  },
]);

const returnGoodsRequestDetail = reactive({
  returnGoodsRequest: {
    "returnGoodsRequestId": "",
    "initiateTime": "",
    "processTime": "",
    "storeId": "",
    "orderId": "",
    "userId": "",
    "status": 0,
    "info": "",
    "returnReason": ""
  }
});

// 获取商店的退款列表
const getReturnGoodsRequestList = async (manageStoreId) => {
  // 发起请求
  let res = await proxy.$api.order.getReturnOrderRequestOfStore(manageStoreId, {
    current: config.current,
    size: config.size,
  });
  // 更新表格数据
  returnGoodsRequestList.value = res.data.returnGoodsRequestList;
  config.total = res.data.total;
};

// 这里直接用索引号找到launchRequestList中的信息即可
const getReturnGoodsRequestDetail = (index) => {
  // 更新原初状态
  originStatus.value = returnGoodsRequestList.value[index].status;
  returnGoodsRequestDetail.returnGoodsRequest = returnGoodsRequestList.value[index];
  detailIndex.value = index;
  dialogVisible.value = true;
}

const changePage = async (page) => {
  loading.value = true;
  config.current = page
  await getReturnGoodsRequestList(manageStoreId.value)
  loading.value = false;
}

let filterTag = (value, row) => {
  return row.status === value;
}

/**
 * 请求审核完毕，保存审核结果
 */
const handleProcessRequest = async () => {
  let res = await ElMessageBox.confirm(
    '确认后将无法更改，确定保存该申请审核吗？',
    '确认审核',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    });
  if (res === 'confirm') {
    let ret = await proxy.$api.order.processReturnOrderRequest(returnGoodsRequestDetail.returnGoodsRequest);
    if (ret.status === 200) {
      ElMessage.success({ showClose: true, message: "退款退货申请处理成功" });
    }
    dialogVisible.value = false;

    loading.value = true;
    await getReturnGoodsRequestList(manageStoreId.value);
    loading.value = false;
  }
}

onMounted(async () => {
  manageStoreId.value = store.state.store.manageStoreId
  loading.value = true;
  await getReturnGoodsRequestList(manageStoreId.value);
  // await getAllCategory();
  loading.value = false;
})
</script>

<style lang="less" scoped>
.table {
  // position: relative;
  // height: 520px;

  .pager {
    position: relative;
    bottom: -15px;
    justify-content: flex-end;
    margin-right: 15px;
    margin-top: 10px;
    margin-bottom: 15px;
  }
}
</style>